package leetcode;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;

/**
 * 描述:39. 组合总和
 *
 * @author Madison You
 * @created 0:25
 */
public class Mid_39_CombinationSum {

    public static void main(String[] args) {
        int[] arr = {2, 3, 6, 7};
        List<List<Integer>> lists = new Mid_39_CombinationSum().combinationSum(arr, 7);
        for (List<Integer> list : lists) {
            System.out.println(Arrays.toString(list.toArray()));
        }
    }

    List<List<Integer>> list = new ArrayList<>();

    List<Integer> temp = new ArrayList<>();

    public List<List<Integer>> combinationSum(int[] candidates, int target) {
        back(candidates, target, 0);
        return list;
    }

    private void back(int[] candidates, int target, int i) {
        if (target < 0) return;
        if (target == 0) {
            list.add(new ArrayList<>(temp));
            return;
        }

        for (int j = i; j < candidates.length; j++) {
            if (candidates[j] <= target) {
                temp.add(candidates[j]);
                int target1 = target - candidates[j];
                back(candidates, target1, j);
                temp.remove(temp.size() - 1); //回溯
            }
        }

        Scanner scanner = new Scanner(System.in);
        scanner.hasNext();
        String next = scanner.next();
    }

}
